import $service from "../../../service.js";
const app = getApp()
var that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    take_type: "1", //取货方式
    serialNum: "", //处方流水号
    hideSite: false, //地址栏显示
    show_choce: 0, //选择地址，药店
    prescriptionDetail: {}, // 处方信息
    siteList: [], //收货地址列表
    isDrugStoreList: true, //是否有合适药店
    isSiteList: true, //地址列表是否为空
    showAddress: false, //显示收货地址
    showDrugStore: false, //显示药店
    groupNo: '', // 药店id
    choiceDrugName: "", //选择药店名称
    choiceDrugAddress: "", //选择药店的地址
    choiceSiteName: "", //收货人名称
    choiceSitePhone: "", //收货人电话
    choiceSiteAddress: "", //收货人地址
    choiceSiteId: "", // 收货地址ID
    choiceSiteAddressDetl: "", //收货人详细地址
    phone: '', // 取件人手机号
    userName: '', // 取件人姓名
    shopOutMessage: '', //缺货信息
    outDistanceMessage: '', //快递超出距离信息
    shippingFee: 0, //运费
    order_detail: [], //药店药品信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    that = this.data;
    this.getReceivingAddress();
  },
  //增加新地址
  addressAdd: function () {
    wx.navigateTo({
      url: "../address_add/address_add"
    })
  },
  // 获取地址列表
  getReceivingAddress() {
    let params = {
      current: 1,
      size: 20
    }
    app.http.getReceivingAddress(params).then(res => {
      let siteList = res.data.list
      let {
        choiceSiteName,
        choiceSitePhone,
        choiceSiteAddress,
        choiceSiteId,
        choiceSiteAddressDetl,
        showAddress
      } = this.data;
      let isSite = ''
      siteList.find((i) => {
        isSite = i.id === this.data.choiceSiteId
      })
      if (!isSite) { // 判断地址有没有被删除 删除了则隐藏重新选择
        showAddress = false
        choiceSiteId = ''
      }
      siteList.forEach(item => {
        if (!this.data.choiceSiteId) { // 如果没有选择地址会选择默认地址
          if (item.addressDeft === '1') {
            choiceSiteName = item.userName;
            choiceSitePhone = item.userPhone;
            choiceSiteAddress = item.address;
            choiceSiteAddressDetl = item.addressDetl;
            choiceSiteId = item.id;
            showAddress = true
          }
        } else { // 如果已经选择了地址 地址被修改后 更新地址
          if (isSite && this.data.choiceSiteId === item.id) {
            choiceSiteName = item.userName;
            choiceSitePhone = item.userPhone;
            choiceSiteAddress = item.address;
            choiceSiteAddressDetl = item.addressDetl;
            showAddress = true
          }
        }
      })
      this.setData({
        siteList,
        choiceSiteId,
        choiceSiteName,
        choiceSitePhone,
        choiceSiteAddress,
        choiceSiteAddressDetl,
        showAddress
      })
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  // 操作收货地址
  siteHandle(e) {
    let {
      id,
      type
    } = e.currentTarget.dataset
    let params = {
      id: id
    }
    if (type === 'updateReceivingAddress') {
      wx.navigateTo({
        url: "../address_add/address_add?id=" + id
      })
    } else {
      if (type === 'setDeftAddress') {
        params.userId = JSON.parse($service.getStore('userInfo')).id
      }
      app.http[type](params).then(res => {
        $service.showToast(res.msg)
        this.getReceivingAddress();
      }).catch(err => {
        $service.showToast(err.msg)
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})